package com.lvchen.common.base;


import android.content.Context;
import android.graphics.Color;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lvchen.R;


/**
 * 
 * @author Joe.xiao
 *
 * @Date 2016年3月29日
 *
 * @Description 封装无结果的UI现实
 */
public class EmptyView {

	private Context mContext;
	private LayoutInflater inflater;
	private ViewGroup mNetworkAbnormalLayout;
	private TextView mNATransparent;
	private TextView mNAText;
	private LinearLayout mNAButtons;
	private ImageView mNAImage;
	private OnClickListener mRetryListener;
	private ImageButton mBtnCreate;

	/**
	 * 无网络UI显示
	 */
	private ImageView mNoNetworkRefresh, mNoNetWork;
	private LinearLayout mNoNetWorkLayout;

	public EmptyView(Context context, LayoutInflater inflater, OnClickListener listener) {
		this.mContext = context;
		this.inflater = inflater;
		this.mRetryListener = listener;
		initNetworkAbnormalLayouts();
	}

	public void setDoRetryListener(OnClickListener listener) {
		this.mRetryListener = listener;
		mNoNetworkRefresh.setOnClickListener(mRetryListener);
	}

	public ViewGroup getView() {
		return mNetworkAbnormalLayout;
	}

	private void initNetworkAbnormalLayouts() {
		mNetworkAbnormalLayout = (ScrollView) inflater.inflate(R.layout.layout_emptyview, null);
		mNetworkAbnormalLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		mNATransparent = (TextView) mNetworkAbnormalLayout.findViewById(R.id.network_abnormal_transparent);

		mNAImage = (ImageView) mNetworkAbnormalLayout.findViewById(R.id.network_abnormal_image_view);
		mNAText = (TextView) mNetworkAbnormalLayout.findViewById(R.id.network_abnormal_text);
		mBtnCreate = (ImageButton) mNetworkAbnormalLayout.findViewById(R.id.btn_create);
		mNAButtons = (LinearLayout) mNetworkAbnormalLayout.findViewById(R.id.network_abnormal_buttons);

		if (mNoNetworkRefresh != null) {
			mNoNetworkRefresh.setOnClickListener(this.mRetryListener);
		}

	}

	public void resetNA2NoData() {
		mNAText.setText(getString(R.string.network_abnormal_text_no_data));
		mNAButtons.setVisibility(View.GONE);
		mNAImage.setVisibility(View.VISIBLE);
	}

	public void resetNA2NODataByRecord(String text) {
		mNAText.setText(text);
		mNAButtons.setVisibility(View.GONE);
		mNAImage.setVisibility(View.GONE);
	}

	public void resetNA2NoDataAndTerriblySorry() {
		mNAText.setText(getString(R.string.network_abnormal_text_no_data));
		mNAButtons.setVisibility(View.GONE);
		mNAImage.setVisibility(View.VISIBLE);
	}

	public void resetNA2NoNetwork() {
		mNAText.setText(getString(R.string.network_abnormal_text_no_network));
		mNAButtons.setVisibility(View.VISIBLE);
		mNAImage.setVisibility(View.GONE);
	}

	/**
	 * @param networkAbnormalTextNoNetwork
	 * @return
	 */
	private CharSequence getString(int networkAbnormalTextNoNetwork) {
		return mContext.getString(networkAbnormalTextNoNetwork);
	}

	public void resetNA2ErrorData() {
		mNAText.setText(getString(R.string.network_abnormal_text_error_data));
		mNAButtons.setVisibility(View.VISIBLE);
		mNAImage.setVisibility(View.GONE);
	}

	public void setTransparentOpaque() {
		mNATransparent.setVisibility(View.VISIBLE);
	}

	public void setErrorResult(int textId) {
		mNAText.setText(textId);
		mNAButtons.setVisibility(View.GONE);
		mNAImage.setVisibility(View.GONE);
	}

	public void setErrorText(int textId) {
		mNAText.setText(textId);
	}

	public void setErrorText(Spanned spanned) {
		mNAText.setText(spanned);
	}

	/**
	 * @param networkAbnormalSelfNoResultSmall
	 */
	public void setImageResource(int networkAbnormalSelfNoResultSmall) {
		mNAImage.setImageResource(networkAbnormalSelfNoResultSmall);
		mNoNetWorkLayout.setVisibility(View.GONE);
		mNAText.setVisibility(View.GONE);
		mBtnCreate.setVisibility(View.GONE);
	}

	public void setContactEmpty(int networkAbnormalSelfNoResultSmall) {
		setImageResource(networkAbnormalSelfNoResultSmall);
		mNoNetWorkLayout.setVisibility(View.GONE);
		mNetworkAbnormalLayout.setBackgroundColor(Color.parseColor("#00000000"));
	}

	public void setEmptyOrCreate(int networkAbnormalSelfNoResultSmall, int createRes, OnClickListener onClickListener) {
		mNAImage.setImageResource(networkAbnormalSelfNoResultSmall);
		mNAText.setVisibility(View.GONE);
		mBtnCreate.setVisibility(View.VISIBLE);
		mNoNetWorkLayout.setVisibility(View.GONE);
		mBtnCreate.setBackgroundResource(createRes);
		mBtnCreate.setOnClickListener(onClickListener);
	}

	public void setEmptyComment() {
		mNoNetWorkLayout.setVisibility(View.GONE);
		mNetworkAbnormalLayout.setVisibility(View.GONE);
		mNAImage.setVisibility(View.GONE);
		mNAText.setVisibility(View.GONE);
	}

	public void setNoNetWork() {
		mNAImage.setVisibility(View.GONE);
		mNAText.setVisibility(View.GONE);
		mBtnCreate.setVisibility(View.GONE);
		mNoNetWorkLayout.setVisibility(View.VISIBLE);
	}

	public void setContactNetWork() {
		mNAImage.setVisibility(View.GONE);
		mNAText.setVisibility(View.GONE);
		mBtnCreate.setVisibility(View.GONE);
		mNetworkAbnormalLayout.setBackgroundColor(Color.parseColor("#00000000"));
		mNoNetWorkLayout.setVisibility(View.VISIBLE);
	}
}
